package com.hzw.saas.service.user.service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.user.ICustomerLevelService;
import com.hzw.saas.api.user.bo.CustomerLevelBO;
import com.hzw.saas.api.user.bo.CustomerLevelExpireBO;
import com.hzw.saas.api.user.query.CustomerLevelGetQuery;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.common.util.ObjFieldUtil;
import com.hzw.saas.common.util.UnitConvertUtils;
import com.hzw.saas.common.util.enums.AssertEnum;
import com.hzw.saas.service.user.mapper.CustomerLevelMapper;
import com.hzw.saas.service.user.model.CustomerLevel;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;

/**
 * <p>
 * 客户信息表 服务实现类
 * </p>
 *
 * @author zzl
 * @since 2021-04-03
 */
@Service("customerLevelServiceImpl")
@RequiredArgsConstructor
@Slf4j
public class CustomerLevelServiceImpl extends ServiceImpl<CustomerLevelMapper, CustomerLevel> implements ICustomerLevelService, IService<CustomerLevel> {

    private final MapperFacade mapperFacade;

    @Override
    public boolean existCustomerLevel(CustomerLevelGetQuery query, AssertEnum throwOn) {
        if (ObjFieldUtil.isAllNullFields(query, ObjFieldUtil.ConvertNull.BLANK)) {
            AssertUtil.assertThrow("会员等级不存在", Objects.equals(throwOn, AssertEnum.FOUND));
            return false;
        }
        CustomerLevel entity = mapperFacade.map(query, CustomerLevel.class);
        int count = this.lambdaQuery().setEntity(entity).count();
        AssertUtil.assertThrow("会员等级已存在: " + JSONUtil.toJsonStr(query), count > 0 && Objects.equals(throwOn, AssertEnum.NOT_FOUND));
        AssertUtil.assertThrow("会员等级不存在: " + JSONUtil.toJsonStr(query), count == 0 && Objects.equals(throwOn, AssertEnum.FOUND));
        return count > 0;
    }

    @Override
    public List<CustomerLevelBO> listLevels() {
        List<CustomerLevel> list = this.lambdaQuery().orderByAsc(CustomerLevel::getRankOrder).list();
        return mapperFacade.mapAsList(list, CustomerLevelBO.class);
    }

    @Override
    public List<CustomerLevelExpireBO> listLevelsWithExpireTime() {
        List<CustomerLevel> list = this.lambdaQuery().orderByAsc(CustomerLevel::getRankOrder).list();
        List<CustomerLevelExpireBO> resultList = MapperUtil.nf().mapAsList(list, CustomerLevelExpireBO.class);
        // set expire time
        Date now = new Date();
        for (CustomerLevelExpireBO result : resultList) {
            this.setExpireTime(result, now);
        }
        return resultList;
    }

    @Override
    public CustomerLevelBO getLevelById(String id) {
        if (StringUtils.isBlank(id)) return null;
        CustomerLevel customerLevel = this.getById(id);
        return mapperFacade.map(customerLevel, CustomerLevelBO.class);
    }

    @Override
    public CustomerLevelBO getLevelByName(String level) {
        if (StringUtils.isBlank(level)) return null;
        List<CustomerLevel> list = this.lambdaQuery().eq(CustomerLevel::getLevelText, level).list();
        if (list.size() == 0)
            return null;
        return mapperFacade.map(list.get(0), CustomerLevelBO.class);
    }

    @Override
    public CustomerLevelExpireBO getLevelWithExpireTime(String id) {
        if (StringUtils.isBlank(id)) return null;
        CustomerLevel customerLevel = super.getById(id);
        CustomerLevelExpireBO result = MapperUtil.nf().map(customerLevel, CustomerLevelExpireBO.class);
        // set expire time
        Date now = new Date();
        this.setExpireTime(result, now);
        return result;
    }

    @Override
    public boolean updateLevel(CustomerLevelBO level2Update) {
        if (ObjFieldUtil.isAllNullFields(level2Update, ObjFieldUtil.ConvertNull.EMPTY))
            return false;
        AssertUtil.assertThrow("levelId为空", StrUtil.isBlank(level2Update.getLevelId()));
        CustomerLevel entity = MapperUtil.nf().map(level2Update, CustomerLevel.class);
        return super.updateById(entity);
    }

    @Override
    public boolean deleteLevel(String levelId) {
        AssertUtil.assertThrow("levelId为空", StrUtil.isBlank(levelId));
        return super.removeById(levelId);
    }

    private void setExpireTime(CustomerLevelExpireBO bo, Date now) {
        if (Objects.isNull(now))
            now = new Date();
        bo.setAwsCoreTimeExpireTime(UnitConvertUtils.offsetDatetime(now, bo.getAwsCoreTimeExpire()));
        bo.setHpcCoreTimeExpireTime(UnitConvertUtils.offsetDatetime(now, bo.getHpcCoreTimeExpire()));
        bo.setCloudStorageExpireTIme(UnitConvertUtils.offsetDatetime(now, bo.getCloudStorageExpire()));
    }
}
